package cn.koala.key.stock.service.impl;

import cn.koala.key.api.KeyManagementApi;
import cn.koala.key.common.excel.ExcelUtils;
import cn.koala.key.common.excel.FileUtil;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.entity.KeyInfo;
import cn.koala.key.stock.domain.dto.NewCarDTO;
import cn.koala.key.stock.domain.vo.EnterStockDetailVO;
import cn.koala.key.stock.domain.vo.NewCarKeyInfoExcel;
import cn.koala.key.stock.service.EnterStockNewCarService;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultCode;
import cn.seed.common.core.ResultGenerator;
import cn.seed.common.core.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
/**
 * 新车业务逻辑
 * @description
 * @author shuai.xu
 * @time 2019/5/20 13:55
*/
@Service
public class EnterStockNewCarServiceImpl implements EnterStockNewCarService {

    @Override
    public Result importNewCar(MultipartFile file) {
        List<NewCarDTO> newCarDTOList = null;
        try {
            newCarDTOList = ExcelUtils.importExcel(file,NewCarDTO.class,0);
            //newCarDTOList = FileUtil.importExcel(file, 0, 1, NewCarDTO.class);
        } catch (Exception e) {
            System.out.println(e);
            return ResultGenerator.genFailResult(ResultCode.FAIL, "导入excel异常！");
        }
        if(null==newCarDTOList||newCarDTOList.isEmpty()){
            throw new ServiceException(ResultCode.BAD_REQUEST,  "excel数据异常请检查！！！");
        }
        Result importNewCar = FeignUtils.getHttpApi(KeyManagementApi.class).importNewCar(newCarDTOList);
        return importNewCar;
    }

    @Override
    public Result submit(String orderNo) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).newCarSubmit(orderNo);
    }

    @Override
    public Result invalid(String orderNo) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).newCarInvalid(orderNo);
    }

    @Override
    public Result delete(String keyBarCode) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).newCarDelete(keyBarCode);
    }

    @Override
    public Result<EnterStockDetailVO> selectNewCarDetail(String orderNo) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).selectNewCarDetail(orderNo);
    }

    @Override
    public Result<List<KeyInfo>> selectNewCarKeyInfo(String orderNo) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).selectNewCarKeyInfo(orderNo);
    }

    @Override
    public List<NewCarKeyInfoExcel> dataProcessing(List<KeyInfo> data) {
        List<NewCarKeyInfoExcel> resultList = new ArrayList<>();
        data.forEach(keyInfoVO -> {
            NewCarKeyInfoExcel keyInfoExcel = new NewCarKeyInfoExcel();
            keyInfoExcel.setBrandSeriesName(keyInfoVO.getBrandName()+keyInfoVO.getSeriesName());
            keyInfoExcel.setCarNo(keyInfoVO.getCarNo());
            keyInfoExcel.setCarVin(keyInfoVO.getCarVin());
            keyInfoExcel.setKeyBarCode(keyInfoVO.getKeyBarCode());
            keyInfoExcel.setWarehouseLocation(keyInfoVO.getWarehouseLocation());
            resultList.add(keyInfoExcel);
        });
        return resultList;
    }
}
